<template>
  <div class="yunqi">
    <!--<mt-header fixed title="小普智能">-->
      <!--<router-link to="/" slot="left">-->
        <!--<mt-button icon="back">返回</mt-button>-->
      <!--</router-link>-->
      <!--<mt-button icon="more" slot="right"></mt-button>-->
    <!--</mt-header>-->
    <div style="margin-top: 5rem">
      <span>添加小普马桶盖</span>
    </div>
    <div style="width: 60%;margin:2rem auto 0 auto">
      <img style="width: 100%" src="../../../../static/img/addto.png" />
    </div>
    <div style="margin: auto">
      <span style="font-size: 0.5rem;">连接电源开机即可连接</span>
    </div>
    <Loading v-if="showLoading" ></Loading>
    <Process v-if="showLoading" :alertText="alertText" ></Process>
    <!--<Inputk-->
      <!--v-if="showAlert"-->
      <!--:showHide="showAlert"-->
      <!--@closeTip="closeTip"-->
      <!--@goToDo="goToDo"-->
      <!--:alertText="alertText"-->
      <!--:LeftText="LeftText"-->
      <!--:RightText="RightText"-->
    <!--&gt;-->
    <!--</Inputk>-->
  </div>
</template>

<script>
  import {mapMutations,mapState} from 'vuex'
  import Loading from '@/components/common/Loading'
  import Process from '@/components/common/Process'
  import Inputk from '@/components/common/Inputk'
  import {Register} from '../../../store/_CRC'

  export default {
    name: 'AddToE',
    data () {
      return {
        showLoading:true,
        alertText:'正在努力注册设备，请稍后',
        LeftText: '',
        RightText: '',
        showAlert: true,
        BANBEN: '',
        deviceId: '',
        YinJBANBEN: 'HC-08'
      }
    },
    components: {
      Loading,
      Process,
      Inputk
    },
    computed:{
      ...mapState([
        // 'getF03String'
        'deviceIdConnect'
      ])
    },
    watch: {
    },
    methods: {
      ...mapMutations([
        'setConnectDeviceId'
        // 'getF03StringConnect'
      ]),
      tolcallback () {
        var self = this
        window.getStorageSync = function (key) {
          switch (key) {
            case 'deviceId':
              // return state.Duuid
              // return "C8:FD:19:58:BC:10"
            // return "3C:A3:08:A9:E3:10";
              return self.deviceId
            case 'serviceId':
              return "0000ffe0-0000-1000-8000-00805f9b34fb"
            case 'characteristicId':
              return "0000ffe1-0000-1000-8000-00805f9b34fb"
          }
        }
        window.setNotify = function ()
        {
          var deviceId = window.getStorageSync('deviceId');
          var serviceId = window.getStorageSync('serviceId');
          var characteristicId = window.getStorageSync('characteristicId');
          window.hilink.notifyBLECharacteristicValueChange(deviceId,serviceId,characteristicId,true);
          console.log('执行成功:11');
          setTimeout('getBenTowrite()',3000)
          return true;
        }
        window.getBenTowrite = function () {
          var deviceId = window.getStorageSync('deviceId');
          var serviceId = window.getStorageSync('serviceId');
          var characteristicId = window.getStorageSync('characteristicId');
          var wrTore = Register.getF03String(1, 78, 1)
          var data = window.writeTo16(wrTore)
          console.log(wrTore)
          var err = window.hilink.writeBLECharacteristicValue(deviceId, serviceId, characteristicId, data,"writeCallback")
        }
        window.writeTo16 = function (dataArr) {
          var newstr = '';
          for(var i = 0;i< dataArr.length;i++) {
            var str16 = (Array(2).join(0) + dataArr[i].toString(16)).slice(-2);
            newstr += str16
          }
          return newstr
        },
          window.readTo10 = function (dataStr) {
          var newArr = new Array
          for(var i = 0; i<dataStr.length;i=i+2) {
            newArr.push(parseInt(dataStr.substring(i,i+2),16))
          }
          return newArr
        },
         window.connetState = function (re)
         {
           var data = eval("("+re+")")
           if(data.connected==true) {
             console.log('连接状态:'+data.connected)
             window.hilink.onBLECharacteristicValueChange ("ValueChange")
             console.log(self.deviceId+'aaa000')
             setTimeout("window.setNotify()", 3000)
           } else {
              console.log('连接失败aaaa')
              console.log(self.deviceId)
              window.hilink.createBLEConnection(self.deviceId)
           }
         },
        window.getCurrentUnregisteredDevice = function (re) {
          var valueC = eval("("+re+")")
          self.deviceId = valueC.deviceId
          // self.setConnectDeviceId({deviceId:deviceid})
          window.hilink.createBLEConnection(self.deviceId)
        }
      }
    },
    mounted () {
      var self = this
      this.tolcallback();
      window.writeCallback = function (re) {
        console.log('写入成功'+re)
      }
      window.callBackRegisterBleDevice = function(recode) {
        if(recode==0) {
          console.log('注册成功')
        } else {
          console.log('注册失败')
        }
      }
      window.readBLECallBack = function(re){
        console.log('yyy')
        console.log(re)
      }
      window.ValueChange = function (re)
      {
        var valueC = eval("("+re+")")
        var data = valueC.data
        var dataArr = window.readTo10(data)
        var zhuBANBEN = (dataArr[4] & 0x00f0) >> 4
        var fuBANBEN = (dataArr[4] & 0x000f)/10
        self.BANBEN = zhuBANBEN + fuBANBEN
        window.hilink.readBLECharacteristicValue(self.deviceId,"0000ffe0-0000-1000-8000-00805f9b34fb","0000ffe1-0000-1000-8000-00805f9b34fb",'readBLECallBack')
        // window.hilink.registerBleDevice(self.deviceId,self.BANBEN,self.YinJBANBEN,'callBackRegisterBleDevice')
      }
      window.hilink.onBLEConnectionStateChange ("connetState")
      window.hilink.getCurrentUnregisteredDevice("getCurrentUnregisteredDevice")
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.yunqi{
  text-align: center;
}
</style>
